/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package jim.h.commons;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.taobao.tddl.client.jdbc.TDataSource;

/**
 * @author jinggang.huangjg 2013-1-8 下午7:46:39
 */
public class TDDLTest {

    private static String sourceRuleString = "<?xml version=\"1.0\" encoding=\"gb2312\"?>"
                                             + "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">"
                                             + "<beans>"
                                             + "<bean id=\"vtabroot\" class=\"com.taobao.tddl.interact.rule.VirtualTableRoot\" init-method=\"init\">"
                                             + "<property name=\"dbType\" value=\"MYSQL\" />"
                                             + "<property name=\"tableRules\">"
                                             + "<map>"
                                             + "<entry key=\"delivery_template\" value-ref=\"deliveryTemplateTableRule\" />"
                                             + "<entry key=\"delivery_sub_template\" value-ref=\"deliverySubTemplateTableRule\" />"
                                             + "<entry key=\"delivery_rate\" value-ref=\"deliveryRateTableRule\" />"
                                             + "<entry key=\"delivery_template_mapping\" value-ref=\"deliveryTemplateMappingTableRule\" />"
                                             + "<entry key=\"delivery_template_migration\" value-ref=\"deliveryTemplateMigrationTableRule\" />"
                                             + "</map>"
                                             + "</property>"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliverySubTemplateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_SUB_TEMPLATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryRateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_RATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateMappingTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_MAPPING_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateMigrationTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_MIGRATION\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />" + "</bean>"
                                             + "</beans>";

    public static void main(String[] args) throws SQLException {
        DataSource dataSource = createDataSource();
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            conn = dataSource.getConnection();
            pstm = conn.prepareStatement("delete from delivery_template where id < 2000000");
            pstm.executeUpdate();
            System.out.println("clear table delivery_template finished");

            pstm = conn.prepareStatement("delete from delivery_sub_template where id < 2000000");
            pstm.executeUpdate();
            System.out.println("clear table delivery_sub_template finished");

            pstm = conn.prepareStatement("delete from delivery_rate where id < 2000000");
            pstm.executeUpdate();
            System.out.println("clear table delivery_rate finished");

            pstm = conn.prepareStatement("delete from delivery_template_mapping where id < 2000000");
            pstm.executeUpdate();
            System.out.println("clear table delivery_template_mapping finished");

            pstm = conn.prepareStatement("delete from delivery_template_migration");
            pstm.executeUpdate();
            System.out.println("clear table delivery_template_migration finished");
        } finally {
            System.out.println("clear tables finished");
            try {
                if (pstm != null) {
                    pstm.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
            }
        }
    }

    private static DataSource createDataSource() {
        TDataSource ds = new TDataSource();
        ds.setAppName("ITUWL_APP");
        ds.setAppRuleString(sourceRuleString);
        ds.init();
        return ds;
    }
}
